package com.sneaker.shower.socket.feature;

import com.sneaker.shower.socket.Channel;
import com.sneaker.shower.socket.repository.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <h1>AbstractChannel</h1>
 * <pre>
 * @Description
 * </pre>
 *
 * @Author lopo
 * @Date 2018/10/17
 */
public abstract class AbstractChannel extends DelegateSignAble implements Channel {
    static Repository<Channel> repository;
    private static Logger logger = LoggerFactory.getLogger(AbstractChannel.class);

    public AbstractChannel(SignAble signAble) {
        super(signAble);
    }

    @Override
    public void start() {
        logger.info("channel started");
    }

    @Override
    public void valid() {
        if (repository.get(this) == null) {
            logger.info("channel valided");
            repository.save(this);
        }
    }

    @Override
    public void invalid() {
        this.send("401");
    }

    @Override
    public void broken() {
        repository.delete(this);
    }
}
